const app = getApp()
const request = require('../../utils/request.js');
Page({
  data: {
    consultationList: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
    baseUrl: '',
  },

  onLoad() {
    this.setData({
      baseUrl: app.globalData.baseUrl
    })
    this.loadConsultationList()
  },

  onShow() {
    this.setData({
      consultationList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadConsultationList()
    })
  },

  // 加载咨询列表
  loadConsultationList() {
    if (this.data.loading || !this.data.hasMore) return
    this.setData({ loading: true })
    // TODO: 调用后端API获取咨询列表
    return new Promise((resolve, reject) => {
      // 构建请求参数
      const params = {
        page: this.data.page,
        pageSize: this.data.pageSize
      }
      request.request({
        url: `${app.globalData.baseUrl}/api/consultation/questions`,
        method: 'GET',
        data: params,
        header: {
          'Authorization': `Bearer ${app.getGlobalToken()}`
        },
        success: (res) => {
          const { list, total } = res.data.data
          this.setData({
            consultationList: [...this.data.consultationList, ...list],
            hasMore: this.data.consultationList.length + list.length < total,
            loading: false
          })
          resolve()
        },
        fail: reject
      })
    })
  },

  // 切换状态标签
  switchStatus(e) {
    const status = e.currentTarget.dataset.status
    if (status === this.data.currentStatus) return

    this.setData({
      currentStatus: status,
      consultationList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadConsultationList()
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      consultationList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadConsultationList()
      wx.stopPullDownRefresh()
    })
  },

  // 上拉加载更多
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    }, () => {
      this.loadConsultationList()
    })
  },

  // 关闭咨询
  closeConsultation(e) {
    const consultationId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定要关闭该咨询吗？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用后端API关闭咨询
          wx.showToast({
            title: '已关闭',
            icon: 'success'
          })
        }
      }
    })
  },

  // 删除咨询
  deleteConsultation(e) {
    const consultationId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定要删除该咨询吗？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用后端API删除咨询
          wx.showToast({
            title: '已删除',
            icon: 'success'
          })
        }
      }
    })
  },

  // 跳转到详情页
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/consultation/detail/detail?id=${id}`
    })
  },

  // 发布新咨询
  goToPublish() {
    wx.navigateTo({
      url: '/pages/consultation/publish/publish'
    })
  },

  // 展开/收起评论
  toggleComments(e) {
    const id = e.currentTarget.dataset.id
    const consultationList = this.data.consultationList
    const index = consultationList.findIndex(item => item.id === id)

    if (index !== -1) {
      consultationList[index].showComments = !consultationList[index].showComments
      this.setData({
        consultationList
      })
    }
  },

  // 添加评论
  addComment(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '添加评论',
      editable: true,
      placeholderText: '请输入评论内容',
      success: (res) => {
        if (res.confirm && res.content) {
          // 模拟添加评论
          request.request({
            url: `${app.globalData.baseUrl}/api/consultation/questions/${id}/comments`,
            method: 'POST',
            data: { content: res.content },
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: (res) => {
              if (res.data.code === 200) {
                wx.showToast({
                  title: '评论成功',
                  icon: 'success'
                })
                this.onPullDownRefresh()
              }
            },
            fail: () => {
              wx.showToast({
                title: '评论失败',
                icon: 'success'
              })
            },
            complete: () => {
              //
            }
          })
        }
      }
    })
  }
}) 